package com.tengju.bff.interfaces.covercharge.controller.provider;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.idanchuang.component.base.JsonResult;
import com.tengju.bff.interfaces.covercharge.constant.*;
import com.tengju.bff.interfaces.covercharge.dto.SignQuery;
import com.tengju.bff.interfaces.covercharge.dto.SignRes;
import com.tengju.bff.interfaces.covercharge.entity.po.ServiceLetaxRegistered;
import com.tengju.bff.interfaces.covercharge.entity.po.ServiceSelfCertification;
import com.tengju.bff.interfaces.covercharge.entity.vo.ServiceSignRecordVo;
import com.tengju.bff.interfaces.covercharge.intf.ISignProvider;
import com.tengju.bff.interfaces.covercharge.service.IServiceSignService;
import com.tengju.bff.interfaces.covercharge.service.IServiceSignRecordService;
import com.tengju.bff.interfaces.covercharge.service.IServiceLetaxRegisteredService;
import com.tengju.bff.interfaces.covercharge.service.IServiceSelfCertificationService;
import com.tengju.bff.interfaces.covercharge.util.DateTimeUtils;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.user.domain.model.archive.sign.SignFromEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import com.tengju.bff.interfaces.covercharge.entity.vo.ServiceSignQuery;

import java.util.Objects;

@Slf4j
@Service("signProvider")
public class SignProvider implements ISignProvider {
    @Autowired
    private IServiceSignService serviceSignService;

    @Autowired
    private IServiceSignRecordService serviceSignRecordService;

    @Autowired
    private IServiceLetaxRegisteredService serviceLetaxRegisteredService;

    @Autowired
    private IServiceSelfCertificationService serviceSelfCertificationService;

    /**
     * 功能描述: <br>
     * 获取星际认证身份
     *
     * @param signQuery
     */
    @Override
    public ApiResponse<SignRes> certificationIdentity(@RequestBody SignQuery signQuery) {
        Assert.notNull(signQuery.getIdCode(), "idCode不能为空");
        try {
            //签约信息
            SignRes signRes=new SignRes();
            ServiceSignQuery serviceSign=serviceSignService.queryFirst(signQuery.getIdCode(), PlatformCodeEnum.DT.getDescriptioin());
            if(Objects.isNull(serviceSign)){
                return ApiResponse.newFailure(CoverChargeErrorCode.BUSINESS_ERROR.getCode(), "没有签约信息");
            }
            if(!serviceSign.getIsSigned().equals(IsSigndEnum.YES.getCode())){
                return ApiResponse.newFailure(CoverChargeErrorCode.BUSINESS_ERROR.getCode(), "还没有签约");
            }
            if(serviceSign.getCurrentIdentity().equals(SignIdentityEnum.person.getCode())){
                return ApiResponse.newFailure(CoverChargeErrorCode.BUSINESS_ERROR.getCode(), "当前身份不符合");
            }
//            ServiceSignRecordVo serviceSignRecordVo=serviceSignRecordService.queryFirst(serviceSign.getId(),
//                    SignFromEnum.SERVICE_PROVIDER.getCode());
//            if(Objects.isNull(serviceSignRecordVo)){
//                return JsonResult.failure(CoverChargeErrorCode.SYS_ERROR.getCode(), "没有签约身份信息");
//            }
//            if(serviceSignRecordVo.getStatus().equals(SignRecordStatusEnum.NO_VALID.getCode())){
//                return JsonResult.failure(CoverChargeErrorCode.SYS_ERROR.getCode(), "签约记录无效");
//            }

            if(serviceSign.getCurrentIdentity().equals(SignIdentityEnum.regist_soho.getCode())){
                //判断乐税有没有申请成功
                QueryWrapper<ServiceLetaxRegistered> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("id_code", signQuery.getIdCode());
                queryWrapper.eq("platform_code", PlatformCodeEnum.DT.getDescriptioin());
                queryWrapper.eq("status", LetaxRegisteredStatusEnum.REGISTRATION_COMPLET.getCode());
                ServiceLetaxRegistered serviceLetaxRegistered=serviceLetaxRegisteredService.getOne(queryWrapper);
                if(Objects.isNull(serviceLetaxRegistered)){
                    return ApiResponse.newFailure(CoverChargeErrorCode.SYS_ERROR.getCode(), "乐税没有认证成功");
                }
                signRes.setUserFrom(ContractFromEnum.LE_TAX_INDIVIDUAL.getFrom());
                signRes.setSignTime(DateTimeUtils.parseLocalDateTimeToString(serviceLetaxRegistered.getCheckAt()));
            }else{
                if(serviceSign.getCurrentIdentity().equals(SignIdentityEnum.certified_business.getCode())){
                    signRes.setUserFrom(ContractFromEnum.SELF_CERT_COMPANY.getFrom());
                }else{
                    signRes.setUserFrom(ContractFromEnum.SELF_CERT_INDIVIDUAL.getFrom());
                }
                //判断自认证有没有申请成功
                QueryWrapper<ServiceSelfCertification> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("id_code", signQuery.getIdCode());
                queryWrapper.eq("platform_code", PlatformCodeEnum.DT.getDescriptioin());
                queryWrapper.eq("type", signRes.getUserFrom());
                queryWrapper.eq("status", SelfCertificationStatus.PASS.getCode());
                ServiceSelfCertification serviceSelfCertification=serviceSelfCertificationService.getOne(queryWrapper);
                if(Objects.isNull(serviceSelfCertification)){
                    return ApiResponse.newFailure(CoverChargeErrorCode.SYS_ERROR.getCode(), "自认证没有认证成功");
                }
                signRes.setSignTime(DateTimeUtils.parseLocalDateTimeToString(serviceSelfCertification.getCheckAt()));
            }
            return ApiResponse.newSuccess(signRes);
        } catch (Exception e) {
            log.error("获取身份信息失败：", e);
            return ApiResponse.newFailure(CoverChargeErrorCode.SYS_ERROR.getCode(), e.getMessage());
        }
    }
}
